Ext.define('GW.controller.BibleReader', {
	extend: 'Ext.app.Controller',
	requires: ['GW.view.Verse'],

	refs: [
		{
			ref: 'bibleTree',
			selector: 'bibletree'
		},
		{
			ref: 'bibleContent',
			selector: 'biblecontent'
		},
		{
			ref: 'bibleReferences',
			selector: '#references'
		}
		,
		{
			ref: 'bibleSearch',
			selector: 'search'
		}
	],

	stores: ['BibleToc','BibleVersion', 'BibleContent', 'Verses'],

	init: function() {
		// Start listening for events on views
		this.control({
			'bibletree #bookCombo': {
				change: function(field, newValue, oldValue, options){
					var chapterCombo = this.getBibleTree().down('#chapterCombo');
					chapterCombo.setDisabled(false);
					chapterCombo.getStore().loadData([{'id':'1','text':'1'},{'id':'2','text':'2'}]);
				}
			},
			'biblecontent versepanel verse': {
				click: function(e, verse){
					this.setClickedVerse(verse);
				},
				loadverse: function(verse, fnCallback){
					var verseId = verse.getId();
					var verseInStore = this.getVersesStore().getById(verseId);
					if(Ext.isObject(verseInStore)){
						// Verse already in store --> No need to load
						fnCallback(verseInStore);
					}
					else{
						GW.model.bible.Verse.load(verseId, {
							scope: this,
							callback: function(loadedVerse) {
								if(Ext.isObject(loadedVerse)){
									fnCallback(loadedVerse);
								}
								else{
									// Verse not found
									verse.set('content','Could not load verse. Please try again later.')
									loadedVerse = verse;
									fnCallback(verse);
								}
								
								// Add verse to store
								this.getVersesStore().add(loadedVerse);
							}
						});
					}
				}
			},
			'biblecontent search': {
				search: function(e, query){
					this.getBibleSearch().setLoading(true);
					var me = this;
					Ext.Ajax.request({
					    url: '/content/bibles.search.json',
					    method: 'GET',
					    disableCaching: false,
					    params: {
					        q: query,
					        b: me.currentBibleVersion.getId()
					    },
					    success: function(response){
					        var text = response.responseText;
					        var searchResults = Ext.JSON.decode(text);
					        var verses = [];
					        
					        // Convert to verse objects
					        Ext.Array.each(searchResults, function(searchResult) {
								verses.push({
				        			xtype: 'verse',
				        			displayMode: 'short',
				        			showTip: true,
				        			highlight: query,
				        			verse: GW.model.bible.Verse.searchResultToVerse(searchResult)
								});
							});
					        me.getBibleSearch().setVerses(verses).setLoading(false);
					    }
					});
				}
			},
			'biblecontent > toolbar > button': {
				click: this.onBibleVersionChanged
			},
			'biblecontent > #bibleView > verse': {
			    click: function(e, verseView){
			    	var verse = verseView.verse;
			    	// Update references
			    	var verses = [];
			    	Ext.Array.each(verse.getReferences(), function(reference){
			    		verses.push({
		        			xtype: 'verse',
		        			displayMode: 'short',
		        			showTip: true,
		        			verse: reference
						});
			    	});
			    	
			    	this.getBibleReferences().setVerses(verses).expand();
			    }
			}
		});
	},
	
	onBibleVersionChanged: function(btn){
		var bibleVersionId = btn.getId();
		
		if(this.setCurrentBibleVersion(this.getBibleVersionStore().getById(bibleVersionId))){
			var items = btn.up().items;
			items.each(function(item){
				item.toggle(false);
			});
			
			btn.toggle(true);
			
			//this.getBibleTree().setRootNode({"id" : bibleVersionId, "text": bibleVersionId});
			this.loadBibleContent();
		}
	},
	
	setCurrentBibleVersion: function(newBibleVersion){
		if(!Ext.isObject(this.currentBibleVersion) || (this.currentBibleVersion.getId() != newBibleVersion.getId())){
			this.currentBibleVersion = newBibleVersion;
			this.getSelectedBibleObject().bible = this.currentBibleVersion.getId();
			return true;
		}
		else{
			return false;
		}
	},
	
	onLaunch: function() {
		var bibleVersionStore = this.getBibleVersionStore();
		// Prevent interactions with the reader until it is loaded
		this.getBibleContent().setLoading(true);
		bibleVersionStore.load({
			callback: this.onBibleVersionsLoaded,
			scope: this
		});
	},
	
	onBibleVersionsLoaded : function(bibles, operation, success){
		if(success){
			// Populate list of bibles
			var toolbarItems = [];
			for(var i=0; i<bibles.length;i++){
				var bible = bibles[i];
				toolbarItems.push({
					text: bible.get('title'),
					id: bible.get('id'),
					xtype: 'button'
				});
			}
			
			var toolbar = {
				xtype: 'toolbar',
				dock: 'top',
				items: toolbarItems
			}
			
			this.getBibleContent().addDocked(toolbar);
			
			// Click first toolbar button --> This will trigger event to load bible content
			var firstBtn = this.getBibleContent().down('toolbar > button');
			firstBtn.fireEvent('click', firstBtn);
		}
		else{
			// Enable interactions with the reader
			this.getBibleContent().update('Could not load bible content.');
			this.getBibleContent().setLoading(false);
		}
	},
	
	setClickedVerse: function(verseView){
		var verse = verseView.verse;
		var selectedBiblePath = verse.get('id');
		if(!Ext.isString(selectedBiblePath)){
			// No current selected bible path --> Init to kjv/gen/1/1
			selectedBiblePath = 'kjv/gen/1/1';
		}
		
		var parts = selectedBiblePath.split('/');
		
		var oldBible = undefined;
		var oldBook = undefined;
		if(Ext.isObject(this.selectedBibleObject)){
			oldBible = this.selectedBibleObject.bible;
			oldBook = this.selectedBibleObject.book;
			oldChapter = this.selectedBibleObject.chapter;
		}
		
		this.selectedBibleObject = {
			'bible' : parts[0] || 'kjv',
			'book' : parts[1] || 'gen',
			'chapter' : parts[2] || '1',
			'verse': parts[3] || '1'
		};
		var reloadBibleContent = (oldBible != this.selectedBibleObject.bible) || (oldBook != this.selectedBibleObject.book) || (oldChapter != this.selectedBibleObject.chapter);
		if(reloadBibleContent){
			this.highlight = verseView.highlight;
			this.loadBibleContent();
		}
		else{
			this.updateHash();
		}
	},
	
	getSelectedBibleObject: function(){
		if(!Ext.isObject(this.selectedBibleObject)){
			this.selectedBibleObject = {
				'bible' : 'kjv',
				'book' : 'gen',
				'chapter' : '1',
				'verse': '1'
			};
		}
		
		return this.selectedBibleObject;
	},
	
	getChapterPath: function(){
		var selectedBibleObject = this.getSelectedBibleObject();
		return selectedBibleObject.bible + '/' + selectedBibleObject.book + '/' + selectedBibleObject.chapter;
	},
	
	loadBibleContent: function(){
		// Disable interactions with the reader
		this.getBibleContent().setLoading(true);
		
		var path = this.getChapterPath();
		
		GW.model.bible.Chapter.load(path, {
			scope: this,
			callback: function(chapter, operation) {
				this.onBibleContentLoaded(chapter);
			}
		});
	},
	
	onBibleContentLoaded: function(chapter){
		this.setCurrentBibleChapter(chapter);
		this.refreshBibleView();
		
		// Enable interactions with the reader
		this.getBibleContent().setLoading(false);
	},
	
	setCurrentBibleChapter: function(newBibleChapter){
		this.currentBibleChapter = newBibleChapter;
	},
	
	getCurrentBibleView: function(){
		var treeObject = this.getSelectedBibleObject();
		var me = this;
		return{
			bible: this.currentBibleVersion.getId(),
			title: this.currentBibleVersion.get('title'),
			book: treeObject.book,
			chapter: treeObject.chapter,
			currentVerse: treeObject.verse,
			verses: this.currentBibleChapter.verses(),
			content: function(){
				var content = [];
				this.verses.each(function(_verse, index) {
					//content += '<a name="'+verse.get('id')+'"><div class="verse" id="'+verse.get('id')+'"><sup class="verseno">('+(index+1)+')</sup>'+verse.get('content')+'</div></a>';
					var item = {xtype: 'verse', highlight: me.highlight, verse: _verse, itemId: _verse.get('id')};
					content.push(item);
				});
				return content;
			}
		};
	},
	
	refreshBibleView: function(){
		var bibleView = this.getCurrentBibleView();
		
		var content = '<div>Current bible version: <b>'+bibleView.title+'</b><br/>';
		content += 'Showing bible entry:<br/>Book: '+bibleView.book+'<br/>Chapter: '+bibleView.chapter+'<br/> Verse: '+bibleView.currentVerse+'</div>';
		
		content = bibleView.content();
		this.highlight = undefined;
		
		this.getBibleContent().down('panel').removeAll();//.update(content);
		this.getBibleContent().down('panel').add(content);
		this.updateHash();
	},
	
	updateHash: function(){
		var bibleView = this.getCurrentBibleView();
		var versePath = bibleView.bible+'/'+bibleView.book+'/'+bibleView.chapter+'/'+bibleView.currentVerse;
		
		var selectedVerse = this.getBibleContent().down('panel').getComponent(versePath);
		if(selectedVerse){
			if(this.lastSelectedVerse){
				this.lastSelectedVerse.unselect();
			}
			this.lastSelectedVerse = selectedVerse;
			selectedVerse.select();
		}
	}
});
